package h0904;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 有效的括号
 * @date 2024/9/4 22:48
 */
public class ValidBracket {
    public static void main(String[] args) {

    }

    static Map<Character, Character> map = new HashMap<>();

    static {
        map.put('(', ')');
        map.put('[', ']');
        map.put('{', '}');
    }

    public boolean isValid(String s) {
        LinkedList<Character> stack = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                stack.addLast(c);
            } else if (!stack.isEmpty()) {
                if (map.get(stack.peekLast()) == c) {
                    stack.removeLast();
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        return stack.isEmpty();
    }
}
